package cn.tedu.blog.server.utils;

import cn.tedu.blog.server.pojo.dto.ArticleAddNewDTO;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class FileUtils {

    //地域节点
    private static final String  endpoint = "oss-cn-chengdu.aliyuncs.com";

    //阿里云账号AccessKey
    private static final String accessKeyId = "LTAI5tBmNrz8XJxc5VY95bQj";

    private static final String accessKeySecret = "ubrCNXAK9bH0xybISzMJ9ENRhoc2Cl";

    //bucket名称
    private static final String bucketName = "laochengdu";

    public static String FileTransForm(InputStream inputStream, String fileUrl) {

        OSS ossClient = null;
        try {
            //创建OSSClient实例
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            //存储对象
            ossClient.putObject(bucketName, fileUrl, inputStream);
        } finally {
            if (ossClient != null) {
                // 关闭OSSClient
                ossClient.shutdown();
            }
        }

        //把新的文件名响应出去
        return "https://"+bucketName+"."+endpoint+"/"+fileUrl;
    }


}
